package com.lj.frame.conf;

import com.lj.commons.reponse.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RequiredArgsConstructor
@Slf4j
@RequestMapping("conf")
@RestController
public class ConfController {

    private final ConfRepository confRepository;


    @ApiOperation(value = "获取配置列表")
    @GetMapping("list")
    public Result getList(@RequestParam(value = "type", required = false, defaultValue = "")String type){
        List<Conf> confList = confRepository.lambdaQuery().eq(StringUtils.hasLength(type), Conf::getType, type).list();
        return Result.ok().setContent("查询成功", confList);
    }


    @PostMapping("save")
    public Result save(@RequestBody List<Conf> confs){
        for(Conf conf : confs){
            Conf has = confRepository.lambdaQuery().eq(Conf::getId, conf.getId()).one();
            if (Objects.nonNull(has)){
                confRepository.updateById(conf);
            }else{
                confRepository.save(conf);
            }
        }
        return Result.ok();
    }


    @PostMapping("remove")
    public Result remove(@RequestBody List<String> ids){
        confRepository.removeByIds(ids);
        return Result.ok();
    }
}
